$(document)
		.ready(
				function() {
					var rowNum = 0;
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					
					/**
					 * 模态窗口关闭时调用
					 */
					$("#orderDetails").on('hide.bs.modal', function() {
						$("#baseTab").addClass("active");
						$("#baseInfor").addClass("active");
						$("#detailTab").removeClass("active");
						$("#detail").removeClass("active");
						$("#returnTab").removeClass("active");
						$("#returnTab").removeClass("#returnDetail");
					});
					/**
					 * 保存与修改
					 */
					$("#optBtn").delegate('#saveBtn', 'click', function() {

						$("#areaInfor").attr("method", "post");

						var areaId = $("#areaId").val();

						if (areaId == "") {
							$("#areaInfor").attr("action", "save.do");
						} else {

							$("#areaInfor").attr("action", "update.do");
						}
						
						
							
							$("#areaInfor").submit();
							
					});
					

					$('#pagination2')
							.jqPaginator(
									{
										totalCounts:totalCount,
										pageSize:pSize,
										visiblePages : pageCount,
										currentPage : 1,
										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var citycode = $("#cityCode").val();
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															citycode:citycode
														},
														cache : false,
														success : function(data) {
															$(
																	"#areaList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var area = results[i];
																var rowContent = "<tr>"
																		+ "<td><input name='areaId' class='checkbox' type='checkbox' value='"
																		+ area.id
																		+ "'/></td>"
																		+ "<td>"
																		+ area.code
																		+ "</td>"
																		+ "<td>"
																		+ area.name
																		+ "</td>"
																		+ "<td>"
																		+ area.citycode
																		+ "</td>"
																		rowContent = rowContent	+ 
																		"<td><button name='editBtn' id='editBtn' objId='"+area.id+"' class='btn btn-info btn-danger' >修改</button></td>"+
																		"<td><button name='lookBtn' id='editBtn' objId='"+area.id+"' class='btn btn-warning btn-large'>查看</button></td>"+
																		+ "</tr>";
																		
																$("#areaList").append(rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});
					

					/**
					 * 全选当前页记录
					 */
					$("#areaList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='areaId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});
					function clearItemTable() {

						$("#itemsTable tr:not(:first)").remove();
					}
					

					/**
					 * 修改按钮事件
					 */

					$("#areaList")
							.delegate(
									'#editBtn',
									'click',
									function() {
										var areaId = $(this).attr("objId");
										
										//获取订单详情
										var editUrl = "show.do?";
										$.ajax({

											url : editUrl,
											type : "POST",
											dataType : 'json',
											data : {
												id : areaId
											},
											cache : false,

											success : function(data) {
												loadArea(data);
											},
											error : function(html) {
												alert("Load Data failly");
												refresh();
											}
										});

									});
					
					
				
				

					$("#tools").delegate('#addArea', 'click', function() {
						var citycode = $("#cityCode").val();
						$("#areaInfor input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);
							

						});
						$("#citycode").val(citycode);
						$("#areaInfors").modal('show');
					});
					/**
					 * 删除
					 */
					$("#tools")
					.delegate(
							'#deleteBtn',
							'click',
							function() {
								delChk = $('input[name="areaId"]:checked');

								if (delChk.length > 0) {
									if (confirm("Are you sure delete the record?")) {

										var ids = "";
										var delUrl = "delete.do";
                                        var idArr=new Array();
                                       
                                        
                                        for (var i = 0; i < delChk.length; i++) {
                                        	idArr.push(delChk.get(i).value);
                                   
									}
										
										$.ajax({
													url : delUrl,
													type: "POST",
													data : {
														ids : idArr.toString()
													},
													cache : false,

													success : function(html) {
														alert("Delete successfully");
														refresh();
													},
													error : function(html) {
														alert("Delete failly");
														refresh();
													}
												});
									} else {
										return;
									}
								} else {
									alert("Please select the record to delete");
									return;
								}
							});

				});

					
function loadArea(area) {
	$("#areaId").val(area.id);
	$("#code").val(area.code);
	$("#name").val(area.name);
	$("#citycode").val(area.citycode);
	$("#areaInfors").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}
